package com.spring.blog.controller;

import com.spring.blog.pojo.dataobject.BlogInfo;
import com.spring.blog.pojo.request.AddRequest;
import com.spring.blog.pojo.request.UpdateRequest;
import com.spring.blog.pojo.response.BlogInfoResponse;
import com.spring.blog.service.BlogService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog")
public class BlogController {
    @Resource(name="blogServiceImpl")
    private BlogService blogService;

//    @Autowired
//    private BlogService blogService;
    @RequestMapping("/getList")

    public List<BlogInfoResponse> getList(){
        log.info("获取博客列表");
        return blogService.getList();
    }

    @RequestMapping("/getBlogDetail")
    public BlogInfoResponse getBlogDetail(@NotNull Integer blogId){
        log.info("获取博客正文,id为:"+blogId);
        return blogService.getBlogDetail(blogId);
    }
    @RequestMapping("/add")
    public boolean add(@RequestBody @Validated AddRequest addRequest){
        log.info("发布博客,addRequest:{}",addRequest.getUserId()+addRequest.getTitle());
        boolean ret=blogService.add(addRequest);
        return ret;

    }
    @RequestMapping("/update")
    public boolean update(@RequestBody @Validated UpdateRequest updateRequest){
        log.info("更新博客，updateRequest:{}",updateRequest.getId());
        boolean ret=blogService.update(updateRequest);
        return ret;

    }
    @RequestMapping("/delete")
    public boolean delete(Integer blogId){
        log.info("删除博客，删除的博客id:"+blogId);
        boolean ret=blogService.delete(blogId);
        return ret;
    }


}
